//
//  FinderListView.swift
//  WYYao
//
//  Created by wangcl on 2024/9/10.
//

import SwiftUI

struct FinderListView: View {
    var list:FinderList
    @State private var isEditing = false
    @State private var isPresentedEditDialog = false
    @State private var isPresentedAddDialog = false
    @State private var selectedItem:FinderItem?
    var body: some View {
        VStack {
            List(selection: $selectedItem) {
                ForEach(list.items,id: \.self) { item in
                    FinderItemView(item: item)
                }
            }.navigationTitle(list.url.lastPathComponent)
                .navigationBarTitleDisplayMode(.inline)
                .toolbar(content: {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {
                            withAnimation {
                                isEditing.toggle()
                            }
                            selectedItem = nil
                        }, label: {
                            Image(systemName: isEditing ? "xmark" : "pencil")
                        })
                    }
                    ToolbarItem(placement: .navigationBarTrailing) {
                        if !isEditing {
                            Button(action: {
                                isPresentedAddDialog.toggle()
                            }, label: {
                                Image(systemName: "plus")
                            })
                        }else{
                            Button(action: {
                                isPresentedAddDialog.toggle()
                            }, label: {
                                Image(systemName: "ellipsis")
                            })
                        }
                        
                    }
                })
        }
    }
}

#Preview {
    let url = Bundle.main.url(forResource: "root", withExtension: nil)!
    let list = FinderList(url: url)
    return NavigationView {
        FinderListView(list: list)
    }
}
